//
//  HWCommentspingluView.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/15.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWCommentspingluView.h"
#import "HWpinglunCommentsCell.h"
#import "HWAddcommentsView.h"


@interface HWCommentspingluView()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) UILabel *miahsulabel;
@property(strong,nonatomic) UITableView *mainaTableVie;

@property(strong,nonatomic) UIView *headView;







@end



@implementation HWCommentspingluView




-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, (kScreenH-210)) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}



- (instancetype)initWithFrame:(CGRect)frame {
    
    
    if (self = [super initWithFrame:frame]) {
        
        
       // 添加灰色背景
        [self addgrayView];
        
        [self setupUI];
        
        
        
    }
    
    
    return  self;
    
}




// 刷新界面
- (void)refreshData  {
    
      
     
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
            
            return;
         
         }
       
         self.dataMuArr = [[NSMutableArray alloc]init];
    
    
          NSString *uid = [HWLocallyStoredData getLoginUserID];
           NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
           NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
           NSString *signMD5 = [WMUtil md5HexDigest:signStr];
       
         
           NSString *type = @"1";

           NSDictionary *parametes = @{
                             
                         
                             @"ct":@"app",
                             @"ac":@"video_pinglun",
                             @"uid":uid,
                             @"t" :tString,
                             @"sign" :signMD5,
                             @"id" : self.idStr,
                             @"ios" : @"1",
                            @"member_id":[HWLocallyStoredData getCode],
           
           };


           [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
               
              
               
               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
               

            
               
               if ([codeStr intValue] == 1) {
                  // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
                   
                   self.dataMuArr  = [HWCommentsModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                   
                   [self.mainaTableVie reloadData]; 
                   
       
                   
                 
               } else {
                   
                   
                    // [MBProgressHUD showError:@"收藏失败"];
                     [MBProgressHUD showError:response[@"message"]];
                   
               }
            
               
               
           } failure:^(NSString *error) {
              
               
               

                  [MBProgressHUD showError:@"请求失败"];
               
           }];
       
    
    
    
    
    
    
    
}

//  设置UI
- (void)setupUI {
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(0, 210, kScreenW , (kScreenH-210)+10)];
    self.fenxiangView.backgroundColor =  [UIColor whiteColor];
    self.fenxiangView.layer.cornerRadius = 14;
    self.fenxiangView.layer.masksToBounds = YES;
    [self addSubview:self.fenxiangView];
    [self.fenxiangView addSubview:self.mainaTableVie];
    
    
    
     self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 55)];
     self.headView.backgroundColor = [UIColor whiteColor];
     self.mainaTableVie.tableHeaderView = self.headView;
    
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( 20,0, 200, 55)];
          
         //   label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

           label.textColor = QCmorenzitiHeiSe;
           // label.shadowColor = [UIColor colorWithWhite:.9f alpha:1.0f];
           //label.shadowOffset = CGSizeMake(0.0f, 1.0f);
           label.backgroundColor = [UIColor clearColor];
           //        label.textAlignment = UITextAlignmentLeft;
           label.text = @"全部评论";
          label.text = [HWjianzhiduiObject keylocalization:@"全部评论"];
    
           [self.headView  addSubview:label];
    
    
    
           UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
           button1.frame = CGRectMake(kScreenW-36-10, 0, 36, 55);
          //  [button1 setBackgroundImage:[UIImage imageNamed:@"shutdownGuanbi"] forState:UIControlStateNormal];
         //  button1.backgroundColor = [UIColor redColor];
           [button1 addTarget:self action:@selector(buttonTouxiangTap) forControlEvents:UIControlEventTouchUpInside];
           [button1 setImage:[UIImage imageNamed:@"shutdownGuanbi"] forState:UIControlStateNormal];
    
//           button1.layer.cornerRadius = 14;
//           button1.layer.masksToBounds = YES;
//
           [self.headView addSubview:button1];
    
    
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
               button2.frame = CGRectMake(kScreenW-50-20, (kScreenH-210)-50-40, 50, 50);
              //  [button1 setBackgroundImage:[UIImage imageNamed:@"shutdownGuanbi"] forState:UIControlStateNormal];
             //  button1.backgroundColor = [UIColor redColor];
          [button2 addTarget:self action:@selector(tianjianpinglu) forControlEvents:UIControlEventTouchUpInside];
          [button2 setImage:[UIImage imageNamed:@"pinglun_ico-1"] forState:UIControlStateNormal];
        
    //           button1.layer.cornerRadius = 14;
    //           button1.layer.masksToBounds = YES;
    //
          [self.fenxiangView addSubview:button2];
    
    
    
    
       
       
           
    
    
     
    
  

}



- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//

    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];

    
  //   self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 55)];
    
    
    
    
    
    
}






- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return  self.dataMuArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWpinglunCommentsCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
    
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWpinglunCommentsCell" owner:nil options:nil].firstObject;
        
    }

     cell.model = self.dataMuArr[indexPath.row];
     
    return  cell;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
     HWCommentsModel *mode   = self.dataMuArr[indexPath.row];
    
    CGFloat  h =   [self calculateRowHeight:mode.content fontSize:12];
    
    
    return 60 + h+15;
    return 140 ;
    
    
    
    
}



- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
 //  NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-51-70, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}











- (void)tianjianpinglu {
    
    
    HWAddcommentsView *addcom = [[HWAddcommentsView alloc]initWithFrame:CGRectMake(0, 210, kScreenW, (kScreenH-210))];
    addcom.backgroundColor =  RGB(255, 255, 255);
    addcom.layer.cornerRadius = 12;
    addcom.layer.masksToBounds = YES;
    
    addcom.idStr = self.idStr; 
    [self addSubview:addcom];
    
    
    
}


//点击buton 按钮






//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}




#pragma mark ------- 点击播放
- (void)buttonTouxiangTap {
    
    [self removeFromSuperview];
    
    
}









@end
